package eclipse.util.maptools.properties;
import java.awt.*;
import java.awt.event.*;

import javax.media.opengl.jgloo.jglsh.*;
import javax.swing.*;
import eclipse.util.maptools.*;
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class MapLightingProperties extends JInternalFrame implements ActionListener, ItemListener
{
	private static final long serialVersionUID = 633214183266313375L;
	/** Creates new form MapLightingProperties */
    public MapLightingProperties() {
    	colorChooser = new JColorChooser();
        cmbLight = new JComboBox();
        chkEnabled = new JCheckBox();
        btnCancel = new JButton();
        btnSave = new JButton();

        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        setTitle("Lighting Map Properties");

        final String obj[] = new String[MapEditorApplication.LIGHTCOUNT];
        for(int i = 0; i < obj.length; i++)
        	obj[i] = "Light " + i;
        cmbLight.setModel(new DefaultComboBoxModel(obj));
        cmbLight.addItemListener(this);
        chkEnabled.setText("Enabled");

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(this);

        btnSave.setText("Save");
        btnSave.addActionListener(this);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(colorChooser, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmbLight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chkEnabled)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSave)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbLight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkEnabled)
                    .addComponent(btnCancel)
                    .addComponent(btnSave))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(colorChooser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
        setClosable(true);
    }                       

    private final void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
    	setVisible(false);
    }                                         

    private final void btnSaveActionPerformed(java.awt.event.ActionEvent evt) 
    {
    	final int index = cmbLight.getSelectedIndex();
    	MapEditorApplication.map.setLightEnabled(
    		index, 
    		chkEnabled.isSelected()
    	);    	
    	final Color cColor = colorChooser.getColor();
    	final float a = 0.3f;
    	final float r = cColor.getRed() / 255f;
    	final float g = cColor.getGreen() / 255f;
    	final float b = cColor.getBlue() / 255f;
    	final JGLColor gColor = new JGLColor(a, r, g, b);
    	
    	if(index == 0 && MapEditorApplication.lightSun != null)
    		MapEditorApplication.lightSun.setColor(gColor);    		    
    	
    	setVisible(false);
    }                                       
    private final void cmbLightStateChanged(java.awt.event.ItemEvent evet)
    {    	
    	final int index = cmbLight.getSelectedIndex();
    	final int r,g,b;
    	chkEnabled.setSelected(MapEditorApplication.map.isLightEnabled(index));
    	r = (int)(MapEditorApplication.map.getLightColor(index).getRed() * 255);
    	g = (int)(MapEditorApplication.map.getLightColor(index).getGreen() * 255);
    	b = (int)(MapEditorApplication.map.getLightColor(index).getBlue() * 255);
    	colorChooser.setColor(r, g, b);
    }
    //cancel button
    private final JButton btnCancel;
    //save button
    private final JButton btnSave;
    /** is the light enabled */
    public final JCheckBox chkEnabled;
    /** Combo box for the light */
    public final JComboBox cmbLight;
    /** Color chooser for the light */
    public final JColorChooser colorChooser;    
    
	@Override
	public final void actionPerformed(ActionEvent event) 
	{
		if(event.getSource() == btnCancel)
		{
			btnCancelActionPerformed(event);
		}
		else if(event.getSource() == btnSave)
		{
			btnSaveActionPerformed(event);
		}			
	}
	@Override
	public void itemStateChanged(ItemEvent event) 
	{
		cmbLightStateChanged(event);
	}

}
